/**
 * @file 留单提交弹窗
 * @author malingna
 */
define(function (require, exports, module) {

    'use strict';
    var service = require('common/service');
    var localStorage = require('cc/util/localStorage');

    var allInputEls = [].slice.call(document.querySelectorAll('input'), 0);
        allInputEls.forEach(function (element) {
            element.addEventListener('focus', function (e) {
                e.target.classList.remove('error');
            });
    });

    window.showRegisterModal = function () {
        var modalEl = document.querySelector('.register-modal');
        modalEl.classList.remove('none');
        var modalInputEls = [].slice.call(modalEl.querySelectorAll('input'), 0);
        modalInputEls.forEach(function (element) {
            element.classList.remove('error');
        });
    };

    window.closeRegisterModal = function () {
        document.querySelector('.register-modal').classList.add('none');
    };
    window.closeAutoOpenModal = function () {
        var modalEl = document.querySelector('.auto-open-modal');
        modalEl.classList.add('none');

        if (!window.isRegisted) {
            resetModalTimer(modalEl);
        }
    };

    window.formOrigin = null;
    // 详细试用信息录入
    window.addFullTrial = function (e, origin) {
        window.formOrigin = origin;
        var trialInfo = checkAndGetFormData(e);
        if (trialInfo) {
            var params = {
                company_name: trialInfo.companyName,
                name: trialInfo.name,
                mobile: trialInfo.phoneNum,
                expectations: '',
                province: '',
                city: '',
                expectation_type: ''
            };

            if (localStorage.get('source')) {
                params.source = localStorage.get('source');
                params.ori_url = localStorage.get('oriUrl');
            }

            addConsult(params);
        }
    };

    // 校验并返回录入的信息
    function checkAndGetFormData(e) {
        var result = true;
        var formEl = e.target.parentElement;
        var companyNameEl = formEl.querySelector('input[name="companyName"]');
        var nameEl = formEl.querySelector('input[name="name"]');
        var phoneNumEl = formEl.querySelector('input[name="phoneNum"]');
        var formData = {
            companyName: companyNameEl ? companyNameEl.value : '',
            name: nameEl ? nameEl.value : '',
            phoneNum: phoneNumEl ? phoneNumEl.value : ''
        };

        if (formData.companyName === '') {
            companyNameEl.classList.add('error');
            result = false;
        }
        if (formData.name === '') {
            nameEl.classList.add('error');
            result = false;
        }
        if (formData.phoneNum === '') {
            phoneNumEl.classList.add('error');
            result = false;
        }

        if (!checkPhoneNum(formData.phoneNum)) {
            result = false;
        }

        return result ? formData : false;
    }

    function checkPhoneNum(phone) {
        if (phone === '') { return false; };
        if (!(/^1[3456789]\d{9}$/.test(phone))) {
            $notice({
                type: 'warn',
                content: '请输入正确的手机号~'
            });
            return false;
        } else {
            return true;
        }
    }

    // post
    function addConsult(params) {
        // params.ori_type = 'eduPage2';
        service.post(
            'https://www.baijiayun.com/default/consult/add',
            params
        ).then(function (res) {
            $notice({
                type: 'success',
                content: '提交成功，我们的工作人员会马上与您联系，并为您开通体验账号~'
            });
            clearInputVal();
            closeCurModal();
            // 清除弹窗的定时器
            window.isRegisted = true;
            window.modalInterval ? clearInterval(window.modalInterval) : '';
        });
    }

    function closeCurModal() {
        if(window.formOrigin === 'register-modal') {
            window.closeRegisterModal();
        }
        if(window.formOrigin === 'auto-open-modal') {
            window.closeAutoOpenModal();
        }
    }

    function clearInputVal() {
        var allInputEls = [].slice.call(document.querySelectorAll('input'), 0);
        allInputEls.forEach(function (element) {
            element.value = '';
        });
    }
});

